// const { request } = require("express")

window.onload = function () {
    var form = document.querySelector('form')
    var username = document.querySelector('[name="username"]')
    var password = document.querySelector('[name="password"]')
    form.onsubmit = e => {
        if (!username.value) {
            popup.msg('用户名不能为空！')
            return false
        }
        if (!password.value) {
            popup.msg('密码不能为空！')
            return false
        }


        request({
            method: 'post',
            url: '/front/api/user/login',
            data: {
                username: username.value,
                password: password.value
            }
        }).then(res => {
            let { error_code, msg } = res
            if (error_code === 200) {
                setCookie('username', res.data.username, 7200)
                setCookie('userId', res.data.userId, 7200)
                localStorage.setItem('token', res.data.token)

                popup.msg(msg, () => {
                    location.href = '/'
                })
            } else {
                popup.msg(msg)
            }
        })
        return false
    }
}